package educrypt.gui.components;

import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;

import educrypt.gui.view.CiphersResultView;
import educrypt.gui.view.EducryptFileChooserView;
import educrypt.i18n.Messages;
import educrypt.resources.EduCryptResources;

class CiphersResultPanel extends EducryptPanel implements CiphersResultView {

	private static final long serialVersionUID = 5663331406659305190L;

	private JTextArea txtResult;
	private JButton btnSaveResult;
	private EducryptFileChooser saveFileDialog;

	public CiphersResultPanel() {
		this.setSize(750, 270);
		this.setPreferredSize(this.getSize());
		this.setLayout(null);

		JLabel lblResult = new JLabel(Messages.getString("guiResult"));
		lblResult.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblResult.setBounds(30, 11, 127, 19);
		this.add(lblResult);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(30, 38, 654, 209);
		this.add(scrollPane);

		txtResult = new JTextArea();
		txtResult.setEditable(false);
		scrollPane.setViewportView(txtResult);

		ImageIcon saveIcon = new ImageIcon(
				EduCryptResources.getImageURL("save24.png"));
		btnSaveResult = new JButton(saveIcon);
		btnSaveResult.setBounds(694, 219, 28, 28);
		this.add(btnSaveResult);

		JSeparator separator = new JSeparator();
		separator.setBounds(195, 269, 359, 1);
		this.add(separator);

		saveFileDialog = new EducryptFileChooser(btnSaveResult);
		saveFileDialog.setTxtMode();

		super.setPanelEnabled(false);
	}

	@Override
	public void addListener(ActionListener ctr) {
		this.btnSaveResult.setActionCommand(SAVE_RESULT);
		this.btnSaveResult.addActionListener(ctr);

		this.saveFileDialog.addListener(ctr);
	}

	@Override
	public void setPanelEnabled(boolean b) {
		super.setPanelEnabled(b);
		if (b == false) {
			try {
				txtResult.getDocument().remove(0,
						txtResult.getDocument().getLength());
			} catch (BadLocationException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void setResultText(String text) {
		txtResult.setText(text);
		txtResult.setCaretPosition(0);
	}

	@Override
	public EducryptFileChooserView getSaveFileDialog() {
		return this.saveFileDialog;
	}

}
